package com.tb.controller;

import com.tb.dubbo.ICartItemService;
import com.tb.dubbo.IGoodsService;
import com.tb.pojo.CartItem;
import com.tb.pojo.Img;
import com.tb.pojo.User;
import com.tb.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(value = "/CartItem")
public class CartItemController{
    @Autowired
    private ICartItemService cartItemService;

    @Autowired
    private IGoodsService goodsService;


    /**
     * @api {get} /CartItem/selectCartItem 购物车详情  万旭刚
     * @apiDescription 购物车详情数据展示 &nbsp;&nbsp;<span style="color:#D0254E">[*需要在用户登陆状态下请求]</span>
     * @apiGroup ShopCar API
     *
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess {int} [code] 返回状态码 0:正常 1:参数错误
     * @apiSuccess {string} msg 返回错误信息
     * @apiSuccess {Object} data 返回数据结果集
     * @apiSuccess (data) {String} goodsName 购物车商品名称
     * @apiSuccess (data) {String} color 购物车商品颜色
     * @apiSuccess (data) {String} size 购物车商品尺码 电子产品系统等属性也用尺码表示
     * @apiSuccess (data) {float} price 购物车商品单价
     * @apiSuccess (data) {int} goodsNum 购物车商品数量
     * @apiSuccess (data) {float} totalPrice 购物车单条商品总价  结果为折扣后的价格
     * @apiSuccess {list} img 返回的商品图片集
     * @apiSuccess (img) {String} imgUrl 购物车商品图片地址
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "msg": "登录成功",
     *     "data": [
     *         {
     *              "goodsId":"1"
     *             "goodsName": "iPhone10",
     *             "color": "黑色",
     *             "size": "5.0",
     *             "price": 5000,
     *             "goodsNum": 2,
     *             "totalPrice": 8000,
     *             "img":[
     *                 {
     *                     imgUrl:"//img12.360buyimg.com/n1/s800x1026_jfs/t25570/143/2486183404/271377/9e74a22d/5be6a1c2N559da3f5.jpg!cc_800x1026.jpg"
     *                 },{
     *                     imgUrl:"https://img14.360buyimg.com/n0/jfs/t1/17309/16/2136/231494/5c19b7bfEc5b297bc/ae0039465606955d.jpg"
     *                 }
     *             ]
     *         },
     *         {
     *             "goodsId":"2"
     *             "goodsName": "iPhone10",
     *             "color": "黑色",
     *             "size": "4.3",
     *             "price": 5000,
     *             "goodsNum": 2,
     *             "totalPrice": 8000,
     *             "img":[
     *                 {
     *                     imgUrl:"//img12.360buyimg.com/n1/s800x1026_jfs/t25570/143/2486183404/271377/9e74a22d/5be6a1c2N559da3f5.jpg!cc_800x1026.jpg"
     *                 },
     *                 {
     *                     imgUrl:"//img12.360buyimg.com/n1/s800x1026_jfs/t25570/143/2486183404/271377/9e74a22d/5be6a1c2N559da3f5.jpg!cc_800x1026.jpg"
     *                 }
     *             ]
     *         }
     *    }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 999,
     *     "msg": "登陆超时",
     *     "data": null
     * }
     *
     * @apiSampleRequest /CartItem/selectCartItem
     */
    @GetMapping(value = "/selectCartItem")
    public Msg selectAllCartItem(HttpServletRequest request){
        try{
            HttpSession session = request.getSession();
            User user = (User)session.getAttribute("User");
            String userId = user.getUserId();
            List<CartItem> list = cartItemService.selectAllCartByUser(userId);
            for (CartItem cartItem : list){
                String goodsId = goodsService.getId(cartItem.getGoodsName());
                List<Img> listPic = cartItemService.selectPicByGoodsId(goodsId);
                cartItem.setImg(listPic);
            }
            return Msg.success(list);
        }catch (Exception e){
            return Msg.error(999,"登陆超时");
        }
    }
}



